//
//  NotesDetailViewController.m
//  TravelFirst
//
//  Created by lanou on 15/9/21.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "NotesDetailViewController.h"
#import "NotesDetailOperationView.h"
#import "NotesModel.h"
#import "NotesDetailTableViewCell.h"
#import "NotesDetailHeaderView.h"
#import "BlurImageView.h"
#import "BigWithDragImageView.h"
#import "NotesCommentTableViewController.h"
#import "NotesAllCommentsViewController.h"
#define kImageViewHeight 250

@interface NotesDetailViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NotesDetailHeaderView *headerView;
@property (nonatomic, strong) BigWithDragImageView *bigImageView;

@end

@implementation NotesDetailViewController

// 选中一行
- (void)selectRow
{
    if (self.pictureStr) {
        NSInteger index = 0;
        for (int i = 0; i < self.dataArray.count; i++) {
            NotesModel *model = self.dataArray[i];
            if ([model.picture isEqualToString:self.pictureStr]) {
                index = i;
            }
        }
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
        //[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
    }
}


#pragma mark - 加载数据 -

- (void)reloadView
{
    NSString *urlStr = [kNotesDetail stringByReplacingOccurrencesOfString:@"参数" withString:self.log_id];
    NSLog(@"%@", urlStr);
    [LORequestManger GET:urlStr success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *postsDic in dic[@"log"][@"posts"]) {
            NotesModel *notesModel = [[NotesModel alloc] init];
            notesModel.posts_id = postsDic[@"id"];
            [notesModel shareJsonWithDictionary:dic[@"log"]];
            notesModel.log_id = dic[@"log"][@"id"];
            [notesModel shareJsonWithDictionary:dic[@"log"][@"created_by"]];
            [notesModel shareJsonWithDictionary:postsDic];
            notesModel.picture = postsDic[@"pictures"][0][@"picture"];
            notesModel.home_fav_count = dic[@"log"][@"fav_count"];
            notesModel.posts_description = postsDic[@"description"];
            [self.dataArray addObject:notesModel];
        }
        [self.bigImageView setValueWithModel:self.dataArray[0]];
        [self.headerView setValueWithModel:self.dataArray[0]];
        
        [self.tableView reloadData];
#warning 放到这里不知道还会不会蹦....
        [self selectRow];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}


#pragma mark - viewDidLoad -
// 隐藏navigationBar和tabBar
- (void)viewWillAppear:(BOOL)animated
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.navigationController.navigationBar.alpha = 0;
    self.navigationController.tabBarController.tabBar.alpha = 0;
}

// 不可少
- (void)viewWillDisappear:(BOOL)animated
{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}

- (void)loadView
{
    [super loadView];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.contentInset = UIEdgeInsetsMake(kImageViewHeight, 0, 0, 0);
    
    self.tableView.backgroundColor = kColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 加载数据
    [self reloadView];
    
    [self.tableView registerClass:[NotesDetailTableViewCell class] forCellReuseIdentifier:@"NotesDetailTableViewCell"];
    
    [self setDragView];
    [self setTableViewHeaderView];
    [self setTableViewFooterView];
    
    NotesDetailOperationView *detailOperationView = [[NotesDetailOperationView alloc] initWithFrame:CGRectMake(0, kHeight - 45, kWidth, 45) target:self action:@selector(detailOperationView:)];
    [self.view addSubview:detailOperationView];
    
    [self setBackButton];

}

// 拖拽变大视图
- (void)setDragView
{
    self.bigImageView = [[BigWithDragImageView alloc] initWithFrame:CGRectMake(0, -kImageViewHeight, kWidth, kImageViewHeight)];
    self.bigImageView.autoresizesSubviews = YES;
    
    [self.bigImageView.nicknameButton addTarget:self action:@selector(nicknameButtonMessage:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.tableView addSubview:self.bigImageView];
}


// 设置TableViewHeader
- (void)setTableViewHeaderView
{
    self.headerView = [[NotesDetailHeaderView alloc] initWithFrame:CGRectMake(0, kImageViewHeight, kWidth, 90)];
    self.tableView.tableHeaderView = self.headerView;
}

// 设置TableViewFooter
- (void)setTableViewFooterView
{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 40)];
    UILabel *endLabel = [[UILabel alloc] initWithFrame:CGRectMake((kWidth - 100) / 2, 10, 100, 30)];
    endLabel.text = @"· The End";
    endLabel.textAlignment = NSTextAlignmentCenter;
    [footerView addSubview:endLabel];
    UIView *leftLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 25, (kWidth - 100) / 2, 1)];
    leftLineView.backgroundColor = [UIColor grayColor];
    [footerView addSubview:leftLineView];
    UIView *rightLineView = [[UIView alloc] initWithFrame:CGRectMake((100 + (kWidth - 100) / 2), 25, (kWidth - 100) / 2, 1)];
    rightLineView.backgroundColor = [UIColor grayColor];
    [footerView addSubview:rightLineView];
    
    self.tableView.tableFooterView = footerView;
}

// 返回按钮
- (void)setBackButton
{
    BlurImageView *blurImageView = [[BlurImageView alloc] initWithFrame:CGRectMake(15, 25, 35, 25) target:self action:@selector(back)];
    [blurImageView.backButton setBackgroundImage:[UIImage imageNamed:@"icon_back_39x29@2x"] forState:UIControlStateNormal];
    [self.view addSubview:blurImageView];
}

// 返回方法
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NotesDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NotesDetailTableViewCell" forIndexPath:indexPath];
    NotesModel *notesModel = self.dataArray[indexPath.row];
    [cell setValueWithModel:notesModel string:@"NotesDetailViewController"];
    [cell setTarget:self praiseAction:@selector(praise:) commentAction:@selector(comment:) index:indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NotesModel *notesModel = self.dataArray[indexPath.row];
    CGFloat height = 0;
    if (notesModel.posts_description && notesModel.posts_description.length != 0) {
        height = [NotesDetailTableViewCell setFrameWithString:notesModel.posts_description].size.height + 10;
    }
    
    return (225 + height + 35);
}

// 视图拖拽变大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    if (y < -kImageViewHeight) {
        CGRect frame = self.bigImageView.frame;
        frame.origin.y = y;
        frame.size.height = -y;
        self.bigImageView.frame = frame;
    }
}


#pragma mark - 点击方法 -

// 页面最下方的
- (void)detailOperationView:(UIButton *)button
{
    if (button.tag == 101) {
        NSLog(@"点赞");
        button.selected = !button.selected;
        if (button.selected) {
            [button setBackgroundImage:[UIImage imageNamed:@"icon_greated_32x32@2x"] forState:UIControlStateSelected];
        } else {
            [button setBackgroundImage:[UIImage imageNamed:@"icon_great_gray_13x13@3x"] forState:UIControlStateNormal];
        }
    } else if (button.tag == 102) {
        
        NotesAllCommentsViewController *allCommentsVC = [[NotesAllCommentsViewController alloc] init];
        NotesModel *model = self.dataArray[button.tag - 100];
        allCommentsVC.log_id = model.log_id;
        [self.navigationController pushViewController:allCommentsVC animated:YES];
        
    } else {
        NSLog(@"分享");
    }
}

// 图片上的点赞
- (void)praise:(UIButton *)button
{
    NSLog(@"%ld", button.tag);
    NSLog(@"点赞===");
    button.selected = !button.selected;
    if (button.selected) {
        [button setBackgroundImage:[UIImage imageNamed:@"icon_greated_32x32@2x"] forState:UIControlStateSelected];
    } else {
        [button setBackgroundImage:[UIImage imageNamed:@"icon_great_gray_13x13@3x"] forState:UIControlStateNormal];
    }
}

// 图片上的评论
- (void)comment:(UIButton *)button
{
    NotesCommentTableViewController *commentVC = [[NotesCommentTableViewController alloc] init];
    NotesModel *model = self.dataArray[button.tag - 100];
    commentVC.posts_id = model.posts_id;
    [self.navigationController pushViewController:commentVC animated:YES];
}

// 顶上的头像进入个人信息
- (void)nicknameButtonMessage:(UIButton *)button
{
    NSLog(@"%ld", button.tag);
    NSLog(@"个人信息");
}



#pragma mark - 懒加载 -

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

@end
